//
//  FSMaoCHDetailViewController.m
//  PASystem
//
//  Created by 刘沿 on 16/8/15.
//  Copyright © 2016年 czd. All rights reserved.
//

#import "FSMaoCHDetailViewController.h"
#import "MaoCuHuiDetailModel.h"
#import "MaoCuHuiView2.h"
#import "DetailCellModel.h"
#import "FSDetailTableViewCell.h"


@interface FSMaoCHDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic,strong) NSMutableArray *dataArray2;


@property (nonatomic,strong) UITableView *tabV;

@property (nonatomic,strong) UITableView *tab2;

@property (nonatomic,strong) UIView *view3;

@property (nonatomic,strong) UIView *btnV;

@end

@implementation FSMaoCHDetailViewController

static NSString *const reuserID = @"MAOCUCELL";

- (NSMutableArray *)dataArray{

    if (_dataArray == nil) {
        
        _dataArray = [NSMutableArray array];
    }

    return _dataArray;
}

- (NSMutableArray *)dataArray2{
    if (_dataArray2 == nil) {
        
        _dataArray2 = [NSMutableArray array];
    }
    return _dataArray2;

}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"贸促会详情";
    
    CGFloat H = self.view.height *2 / 5;
    
    UITableView *tabV = [[UITableView alloc] init];
    
    self.tabV = tabV;
    
    tabV.dataSource = self;
    
    tabV.delegate = self;
    
    [self.view addSubview:tabV];
    
    [tabV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view.mas_top);
        
        make.left.equalTo(self.view.mas_left);
        
        make.right.equalTo(self.view.mas_right);
        
        make.height.equalTo(@(H));
    }];
    
   
    [self loadData];
    
}

//加载网络数据
- (void)loadData{

    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json = [NSString stringWithFormat:@"{'action': mchMch,'service': MemberService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,id:%@ }'}",userId,pwdId,userHumber,self.ID];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        
        NSDictionary *dict1 = JSON[@"reason"];
        
        NSDictionary *dict2 = dict1[@"attrs"];

        MaoCuHuiDetailModel *model = [MaoCuHuiDetailModel mj_objectWithKeyValues:dict2];
        
        [self.dataArray addObject:model];
        
        NSArray *arr = JSON[@"responsible"];
        
        for (NSDictionary *dict in arr) {
            
            NSDictionary *resDict = dict[@"attrs"];
            
            DetailCellModel *mod = [DetailCellModel mj_objectWithKeyValues:resDict];
            
            [self.dataArray2 addObject:mod];
        }
        [self setUP:self.dataArray2];
                [self.tabV reloadData];

    } failure:^(NSError *error) {
        
    }];

}

//设置详情页面
- (void)setUP:(NSMutableArray *)arr{
//按钮的父控件
    UIView *btnView = [[UIView alloc] init];
    
    self.btnV = btnView;
    
    btnView.backgroundColor = [UIColor grayColor];
    
    [self.view addSubview:btnView];
    
    [btnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tabV.mas_bottom);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.height.equalTo(@44);
    }];
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [btnView addSubview:btn1];
    
    [btn1 setTitle:@"主要负责人信息" forState:UIControlStateNormal];
    
    [btn1 addTarget:self action:@selector(btn1Click) forControlEvents:UIControlEventTouchUpInside];
    
    [btn1.titleLabel setFont:[UIFont systemFontOfSize:14]];
    
    [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(btnView.mas_top);
        make.left.equalTo(btnView.mas_left);
        make.bottom.equalTo(btnView.mas_bottom);
        make.width.equalTo(@100);
    }];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [btnView addSubview:btn2];
    
    [btn2 setTitle:@"申报批准情况" forState:UIControlStateNormal];
    [btn2.titleLabel setFont:[UIFont systemFontOfSize:14]];

    [btn2 addTarget:self action:@selector(btn2Click) forControlEvents:UIControlEventTouchUpInside];
    
    [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(btnView.mas_top);
        make.left.equalTo(btn1.mas_right);
        make.bottom.equalTo(btnView.mas_bottom);
        make.width.equalTo(@100);
    }];
//存放tab
    UIView *view3 = [[UIView alloc] init];
    
    self.view3 = view3;
    
    [self.view addSubview:view3];
    
    [view3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(btnView.mas_bottom);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(self.view.mas_bottom).mas_offset(-49);
    }];
    
    UITableView *tab2 = [[UITableView alloc] init];
    
    tab2.delegate = self;
    
    tab2.dataSource = self;
    
    self.tab2 = tab2;
    
    [view3 addSubview:tab2];
    
    tab2.backgroundColor = [UIColor whiteColor];
    
    [tab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(btnView.mas_bottom);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(self.view.mas_bottom).mas_offset(-49);
    }];
    
    UILabel *lbl = [[UILabel alloc] init];
    
    lbl.hidden = YES;
    
    lbl.text = @"暂时还没有数据哦";
    
    [view3 addSubview:lbl];
    
    [lbl mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(view3.mas_centerX);
        make.centerY.equalTo(view3.mas_centerY);
        make.width.equalTo(@200);
        make.height.equalTo(@44);
    }];
    
    if (arr.count == 0) {
        lbl.hidden = NO;
        tab2.hidden = YES;
    }
    
    
    
}
//按钮1 点击
- (void)btn1Click{

    [self.view3.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    [self.view3 addSubview:self.tab2];
    [self.tab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.btnV.mas_bottom);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(self.view.mas_bottom).mas_offset(-49);
    }];
}

//按钮2点击
- (void)btn2Click{

    [self.view3.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    UIScrollView *scr = [[UIScrollView alloc] init];
    scr.backgroundColor = [UIColor whiteColor];
    scr.contentSize = CGSizeMake(self.view.size.width, 300);
    
    [self.view3 addSubview:scr];
    
    [scr mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view3.mas_top);
        make.left.equalTo(self.view3.mas_left);
        make.right.equalTo(self.view3.mas_right);
        make.bottom.equalTo(self.view3.mas_bottom);
    }];
    
    MaoCuHuiView2 *mao1 = [MaoCuHuiView2 WithMaoCuHuiView2];

    MaoCuHuiDetailModel *model = self.dataArray.firstObject;
    
    mao1.model = model;
    
    [scr addSubview:mao1];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (tableView == self.tabV) {
        
        return 15;

    }else{
        
        return self.dataArray2.count;
    
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (tableView == self.tabV) {
        MaoCuHuiDetailModel *model = self.dataArray.firstObject;
        
        if (indexPath.row == 0) {
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 120, 40)];
            
            label.text = @"机构中文名称:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            
            lbl2.font = [UIFont systemFontOfSize:15];
            
            lbl2.text = model.CHNAME;
            [cell.contentView addSubview:lbl2];
            
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(label.mas_centerY);
                make.left.equalTo(label.mas_right);
                make.height.equalTo(label.mas_height);
                make.right.equalTo(cell.contentView.mas_right);
            }];
            
            
            return cell;
            
        }else if (indexPath.row == 1){
            
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 120, 40)];
            
            label.text = @"企业英文名称:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            
            lbl2.font = [UIFont systemFontOfSize:15];
            
            lbl2.text = model.ENAME;
            [cell.contentView addSubview:lbl2];
            
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(label.mas_centerY);
                make.left.equalTo(label.mas_right);
                make.height.equalTo(label.mas_height);
                make.right.equalTo(cell.contentView.mas_right);
            }];
            
            
            return cell;
            
            
        }
        else if (indexPath.row == 2){
            
            
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 40)];
            
            label.text = @"机构建制:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            
            lbl2.font = [UIFont systemFontOfSize:15];
            
            lbl2.text = model.INSTITUTIONAL;
            [cell.contentView addSubview:lbl2];
            
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(label.mas_centerY);
                make.left.equalTo(label.mas_right);
                make.height.equalTo(label.mas_height);
                make.right.equalTo(cell.contentView.mas_right);
            }];
            
            
            return cell;
            
            
        }
        else if (indexPath.row == 3){
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 120, 40)];
            
            label.text = @"机构工作状况:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            
            lbl2.font = [UIFont systemFontOfSize:15];
            
            lbl2.text = model.WORKTYPE;
            [cell.contentView addSubview:lbl2];
            
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(label.mas_centerY);
                make.left.equalTo(label.mas_right);
                make.height.equalTo(label.mas_height);
                make.right.equalTo(cell.contentView.mas_right);
            }];
            
            
            return cell;
            
        }
        else if (indexPath.row == 4){
            
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 150, 40)];
            
            label.text = @"机构中文名称简写:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            
            lbl2.font = [UIFont systemFontOfSize:15];
            
            lbl2.text = model.CHANAME;
            [cell.contentView addSubview:lbl2];
            
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(label.mas_centerY);
                make.left.equalTo(label.mas_right);
                make.height.equalTo(label.mas_height);
                make.right.equalTo(cell.contentView.mas_right);
            }];
            
            
            return cell;
        }
        else if (indexPath.row == 5){
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 150, 40)];
            
            label.text = @"机构英文名称简写:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            
            lbl2.font = [UIFont systemFontOfSize:15];
            
            lbl2.text = model.EANAME;
            [cell.contentView addSubview:lbl2];
            
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(label.mas_centerY);
                make.left.equalTo(label.mas_right);
                make.height.equalTo(label.mas_height);
                make.right.equalTo(cell.contentView.mas_right);
            }];
            
            
            return cell;
            
        }
        else if (indexPath.row == 6){
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 60, 40)];
            
            label.text = @"编制数:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            
            lbl2.font = [UIFont systemFontOfSize:15];
            
            lbl2.text = model.COMPLILATION;
            
            [cell.contentView addSubview:lbl2];
            
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(label.mas_centerY);
                make.left.equalTo(label.mas_right);
                make.height.equalTo(label.mas_height);
                make.right.equalTo(cell.contentView.mas_right);
            }];
            
            
            return cell;
            
        }
        else if (indexPath.row == 7){
            
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 80, 40)];
            
            label.text = @"实有人员:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            
            lbl2.font = [UIFont systemFontOfSize:15];
            
            lbl2.text = model.HAVECOMPILATION;
            [cell.contentView addSubview:lbl2];
            
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(label.mas_centerY);
                make.left.equalTo(label.mas_right);
                make.height.equalTo(label.mas_height);
                make.right.equalTo(cell.contentView.mas_right);
            }];
            
            
            return cell;
        }
        else if (indexPath.row == 8){
            
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 80, 40)];
            
            label.text = @"办公地址:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            
            lbl2.font = [UIFont systemFontOfSize:15];
            
            lbl2.text = model.ADDRESS;
            [cell.contentView addSubview:lbl2];
            
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(label.mas_centerY);
                make.left.equalTo(label.mas_right);
                make.height.equalTo(label.mas_height);
                make.right.equalTo(cell.contentView.mas_right);
            }];
            
            
            return cell;
        }
        else if (indexPath.row == 9){
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
            
            label.text = @"邮编:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            
            lbl2.font = [UIFont systemFontOfSize:15];
            
            lbl2.text = model.CODE;
            [cell.contentView addSubview:lbl2];
            
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(label.mas_centerY);
                make.left.equalTo(label.mas_right);
                make.height.equalTo(label.mas_height);
                make.right.equalTo(cell.contentView.mas_right);
            }];
            
            
            return cell;
            
        }
        else if (indexPath.row == 10){
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 60, 40)];
            
            label.text = @"联系人:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            
            lbl2.font = [UIFont systemFontOfSize:15];
            
            lbl2.text = model.CONTACTS;
            [cell.contentView addSubview:lbl2];
            
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(label.mas_centerY);
                make.left.equalTo(label.mas_right);
                make.height.equalTo(label.mas_height);
                make.right.equalTo(cell.contentView.mas_right);
            }];
            
            
            return cell;
            
        }
        else if (indexPath.row == 11){
            
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
            
            label.text = @"手机:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            
            lbl2.font = [UIFont systemFontOfSize:15];
            
            lbl2.text = model.PHONE;
            [cell.contentView addSubview:lbl2];
            
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(label.mas_centerY);
                make.left.equalTo(label.mas_right);
                make.height.equalTo(label.mas_height);
                make.right.equalTo(cell.contentView.mas_right);
            }];
            
            
            return cell;
        }
        else if (indexPath.row == 12){
            
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
            
            label.text = @"电话:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            
            lbl2.font = [UIFont systemFontOfSize:15];
            
            lbl2.text = model.TEL;
            [cell.contentView addSubview:lbl2];
            
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(label.mas_centerY);
                make.left.equalTo(label.mas_right);
                make.height.equalTo(label.mas_height);
                make.right.equalTo(cell.contentView.mas_right);
            }];
            
            
            return cell;
        }else if (indexPath.row == 13){
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
            
            label.text = @"传真:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            
            lbl2.font = [UIFont systemFontOfSize:15];
            
            lbl2.text = model.FOX;
            [cell.contentView addSubview:lbl2];
            
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(label.mas_centerY);
                make.left.equalTo(label.mas_right);
                make.height.equalTo(label.mas_height);
                make.right.equalTo(cell.contentView.mas_right);
            }];
            
            
            return cell;
            
        }else if (indexPath.row == 14){
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
            
            label.text = @"邮箱:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            
            lbl2.font = [UIFont systemFontOfSize:15];
            
            lbl2.text = model.EMAIL;
            
            [cell.contentView addSubview:lbl2];
            
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(label.mas_centerY);
                make.left.equalTo(label.mas_right);
                make.height.equalTo(label.mas_height);
                make.right.equalTo(cell.contentView.mas_right);
            }];
            
            
            return cell;
            
        }else{
            
            return nil;
            
        }
    }else if(tableView == self.tab2){
    
        DetailCellModel *model = self.dataArray2[indexPath.row];
        
        NSString *ID = @"DetailTableView";
        
        FSDetailTableViewCell *cell = [FSDetailTableViewCell WithDetailTableViewCell];
        
        if (cell == nil) {
            
            cell = [[FSDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        
        cell.model = model;
        
        return cell;
    }else{
    
        return nil;
    }
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (tableView == self.tabV) {
        
        return 44;
    }else{
    
        return 170;
    }


}




@end
